package main

import (
	"fmt"
	"golang.org/x/net/http2"
	"golang.org/x/net/http2/h2c"
	"log"
	"net/http"
	"os"
)

func main() {
	path, _ := os.Getwd()
	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path == "/1" { // 当URL路径为/1时触发关闭连接
			w.WriteHeader(http.StatusResetContent) // 重置http状态，关闭连接，这将(可能)触发RST_STREAM帧的发送，但不是直接发送RST_STREAM帧，没有提供api直接操作RST_STREAM帧
			log.Println("RST_STREAM frame sent")   // 打印日志
			return
		}

		fmt.Fprint(w, "Hello world   ")
		fmt.Fprintf(w, "Protocol: %s\n", r.Proto)
	})
	h2s := &http2.Server{
		// ...
	}
	h1s := &http.Server{
		Addr:    ":8080",
		Handler: h2c.NewHandler(handler, h2s),
	}
	log.Fatal(h1s.ListenAndServeTLS(path+"\\certificate.pem", path+"\\private.pem"))
}
